package system

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type Device_groupService struct{}

// CreateDevice_group 创建设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) CreateDevice_group(ctx context.Context, device_group *system.Device_group) (err error) {
	err = global.GVA_DB.Create(device_group).Error
	return err
}

// DeleteDevice_group 删除设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) DeleteDevice_group(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&system.Device_group{}, "id = ?", ID).Error
	return err
}

// DeleteDevice_groupByIds 批量删除设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) DeleteDevice_groupByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.Device_group{}, "id in ?", IDs).Error
	return err
}

// UpdateDevice_group 更新设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) UpdateDevice_group(ctx context.Context, device_group system.Device_group) (err error) {
	err = global.GVA_DB.Model(&system.Device_group{}).Where("id = ?", device_group.ID).Updates(&device_group).Error
	return err
}

// GetDevice_group 根据ID获取设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) GetDevice_group(ctx context.Context, ID string) (device_group system.Device_group, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&device_group).Error
	return
}

// GetDevice_groupInfoList 分页获取设备分组记录
// Author [yourname](https://github.com/yourname)
func (device_groupService *Device_groupService) GetDevice_groupInfoList(ctx context.Context, info systemReq.Device_groupSearch) (list []system.Device_group, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.Device_group{})
	var device_groups []system.Device_group
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&device_groups).Error
	return device_groups, total, err
}
func (device_groupService *Device_groupService) GetDevice_groupPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
